package com.feicheng.lanniao.service.impl;

import com.feicheng.lanniao.entity.House;
import com.feicheng.lanniao.entity.HouseArea;
import com.feicheng.lanniao.entity.HouseLease;
import com.feicheng.lanniao.entity.HouseLeaseClientVo;
import com.feicheng.lanniao.entity.HouseLeaseOwner;
import com.feicheng.lanniao.entity.HouseLeasePrice;
import com.feicheng.lanniao.entity.HouseSell;
import com.feicheng.lanniao.entity.HouseSellClientVo;
import com.feicheng.lanniao.entity.HouseSellPrice;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.mapper.HouseLeaseClientVoMapper;
import com.feicheng.lanniao.mapper.HouseMapper;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.IHouseAreaService;
import com.feicheng.lanniao.service.IHouseLeaseClientVoService;
import com.feicheng.lanniao.service.IHouseLeaseOwnerService;
import com.feicheng.lanniao.service.IHouseLeasePriceService;
import com.feicheng.lanniao.service.IHouseLeaseService;
import com.feicheng.lanniao.service.IHouseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;

import com.tencentcloudapi.cloudaudit.v20190319.CloudauditClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

import javax.swing.*;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-07
 */
@Service
public class HouseLeaseClientVoServiceImpl extends ServiceImpl<HouseLeaseClientVoMapper, HouseLeaseClientVo> implements IHouseLeaseClientVoService {
	@Autowired
	private IHouseLeasePriceService iHouseLeasePriceService;
	@Autowired
	private IHouseService iHouseService;
	@Autowired
	private IHouseLeaseService iHouseLeaseService;
	@Autowired
	private IHouseLeaseOwnerService iHouseLeaseOwnerService;
	@Autowired
	private IHouseAreaService iHouseAreaService;
	/*
	 * 租房表的分页查询
	 */
	@Override
	public ResultInfo getList(String des,Integer provienceId,
			   Integer cityId, Integer reignId,
			   Integer clientId, Integer LeaseId, Integer houseAreaId, Integer leasePriceId,
			   String tel,
			   String wxNumber,
			   String louceng,
			   String xiaoqu,
			   String pianqu,
			   String chaoxiang,
			   String zhuangxiu,
			   Integer dianti,
			   String niandai,
			   Integer roomCount,
			   Integer currentPage,
			   Integer pageSize) {
		System.out.println("+++++++++++++"+ currentPage);
		
		QueryWrapper<HouseLeaseClientVo> queryWrapper = new QueryWrapper<HouseLeaseClientVo>();
		queryWrapper.eq("sale_flg", "1");
		// -----------判断描述不为空的情况下---------- 按描述搜索
		if (StringUtils.hasLength(des)) {
			queryWrapper.like("des", des);
		}
		// ----------- 判断面积区间id不为空的情况下----------- 按面积区间检索
		if (null != houseAreaId && houseAreaId != 0) {
		// 得到1个面积区间对象
			HouseArea houseArea = iHouseAreaService.getById(houseAreaId);
		// 在条件中做检索 在面积区间的最小-最大之间做查询
			queryWrapper.between("area", houseArea.getMinArea(), houseArea.getMaxArea());
		// queryWrapper.eq("area", area);//按面积搜索
		}
		// --------------判断价格区间id不为空且不为0-----------按照价格区间检索
		if (null != leasePriceId && leasePriceId !=0) {
		HouseLeasePrice houseLeasePrice = iHouseLeasePriceService.getById(leasePriceId);
			queryWrapper.between("lease_price", houseLeasePrice.getMinPrice(), houseLeasePrice.getMaxPrice());
		}
			queryWrapper.eq("del_flg", "1");
		
		//----------------判断片区是否为空-------
		if(pianqu != null && pianqu !=""){
			queryWrapper.like("pianqu",pianqu);
		}
		
		if(xiaoqu != null && xiaoqu != ""){
			queryWrapper.like("xiaoqu",xiaoqu);
		}
		if(null != dianti && 0 != dianti){
			queryWrapper.like("dianti",dianti);
		}
		
		if(zhuangxiu != null && zhuangxiu != ""){
			queryWrapper.like("zhuangxiu", zhuangxiu);
		}
		
		if(provienceId != null && provienceId != 0){
		System.out.println("_____________________" + provienceId);
			queryWrapper.eq("provience_id", provienceId);
		}
		
		if(cityId != null && cityId != 0){
			queryWrapper.eq("city_id", cityId);
		}
		
		if(reignId != null && reignId != 0){
			queryWrapper.eq("reign_id", reignId);
		}
		
		if(roomCount != null){
			queryWrapper.eq("room_count", roomCount);
		}
		if(louceng != null){
			queryWrapper.like("louceng", louceng);
		}
		if(chaoxiang != null){
			queryWrapper.like("chaoxiang", chaoxiang);
		}

		if (clientId != null && clientId != 0) {
			queryWrapper.eq("client_id",clientId);
		}
		IPage<HouseLeaseClientVo> pageInfo = new Page<HouseLeaseClientVo>(currentPage, pageSize);
		return ResultInfo.success(this.page(pageInfo, queryWrapper));
		}
	/*
	 * 查询租房信息详细信息
	 */
	@Override
	public ResultInfo findDetails(Integer leaseId) {

		return ResultInfo.success(this.getById(leaseId));
	}
	/*
	 * 新增租房信息
	 */
	@Override
	@Transactional(rollbackForClassName={"Exception"})
	public ResultInfo addHouseLeaseClient(Integer leasePrice,   Integer roomCount,
			Integer area, Integer provienceId, Integer cityId, Integer reignId, String imgUrl,
			 Integer clientId,
			String pianqu,
			String xiaoqu,
			String zhuangxiu,
			String chaoxiang,
			Integer dianti,
			String louceng,
			String tel,
			String wxNumber,
			String niandai
			  ) {
		House house = new House();
		HouseLeaseOwner houseLeaseOwner = new HouseLeaseOwner();
		HouseLease houseLease = new HouseLease();
		house.setArea(area);
		house.setRoomCount(roomCount);
		house.setPianqu(pianqu);
		house.setXiaoqu(xiaoqu);
		house.setZhuangxiu(zhuangxiu);
		house.setChaoxiang(chaoxiang);
		house.setDianti(dianti);
		house.setNiandai(niandai);
		if (null == provienceId) {
			throw new SystemException("省不能为空，请输入一下");
		}
		if (null == cityId) {
			throw new SystemException("市不能为空，请输入一下");
		}
		if (null == reignId) {
			throw new SystemException("区不能为空，请输入一下");
		}
		house.setProvienceId(provienceId);
		house.setCityId(cityId);
		house.setReignId(reignId);
		house.setImgUrl(imgUrl);
		iHouseService.save(house);
		houseLease.setLouceng(louceng);
		if (null == leasePrice) {
			throw new SystemException("价格不能为空，请输入一下");
		}
		houseLease.setLeasePrice(leasePrice);
		houseLease.setHouseId(house.getHouseId());
		houseLease.setTel(tel);
		houseLease.setWxNumber(wxNumber);
		iHouseLeaseService.save(houseLease);
		houseLeaseOwner.setLeaseId(houseLease.getLeaseId());
		if(clientId == null){
			throw new SystemException("用户Id不能为空，请输入一下");
		}
		houseLeaseOwner.setClientId(clientId);
		iHouseLeaseOwnerService.save(houseLeaseOwner);
		return ResultInfo.success();
	}
	/*
	 * 更新租房信息
	 */
	@Override
	public ResultInfo updateHouseLease(HouseLeaseClientVo houseLeaseClientVo) {
		HouseLeaseOwner houseLeaseOwner = new HouseLeaseOwner();
		HouseLease houseLease = iHouseLeaseService.getById(houseLeaseClientVo.getLeaseId());
		houseLease.setMethod(houseLeaseClientVo.getMethod());
		houseLease.setLeasePrice(houseLeaseClientVo.getLeasePrice());
		House house = iHouseService.getById(houseLease.getHouseId());
		house.setDes(houseLeaseClientVo.getDes());
		iHouseService.updateById(house);
		iHouseLeaseService.updateById(houseLease);
		return ResultInfo.success();
	}

	/**
	 * @Author tianchunlei
	 * @Description //更新发布的对外出租房屋信息接口，以前的updateHouseLease不在维护
	 * @Date 2021/10/11 13:00
	 * @Param
	 * @return
	 */

	@Override
	public ResultInfo updateHouseLeaseV2(Integer provienceId, Integer cityId, Integer reignId, String pianqu, String xiaoqu, Integer roomCount, String chaoxiang, Integer dianti, String zhuangxiu, String niandai, Integer area, String imgUrl, Integer leaseId, Integer leasePrice, String louceng, String tel, String wxNumber) {

		//需要修改两张表，一张发布出租信息表，一张房屋基础信息表
		/*
		修改发布出租房屋信息表
		 */
		HouseLease houseLease = new HouseLease();
		houseLease.setLeaseId(leaseId);//租赁id 主键
		houseLease.setLeasePrice(leasePrice);//租赁价格
		houseLease.setLouceng(louceng);//楼层
		houseLease.setTel(tel);//电话
		houseLease.setWxNumber(wxNumber);//微信
		iHouseLeaseService.updateById(houseLease);
		/*
		修改房屋房屋信息表
		 */
		//1.先按照发布id查询房屋基础表的id
		HouseLease byId = iHouseLeaseService.getById(leaseId);
		House house = iHouseService.getById(byId.getHouseId());
		System.out.println(house.getHouseId());
		//然后封装house
		house.setProvienceId(provienceId); //省编号
		house.setCityId(cityId);//市编号
		house.setReignId(reignId); //区编号
		house.setPianqu(pianqu); //片区
		house.setXiaoqu(xiaoqu); //小区
		house.setRoomCount(roomCount); //几室
		house.setChaoxiang(chaoxiang); // 朝向
		house.setDianti(dianti); //电梯
		house.setZhuangxiu(zhuangxiu); //装修
		house.setNiandai(niandai); //建筑年代
		house.setArea(area); //面积
		house.setImgUrl(imgUrl); //图片
		iHouseService.updateById(house);
		return ResultInfo.success();
	}

	/*
	 * 删除租房信息
	 */
	@Override
	public ResultInfo deleteHouseLeaseClient(HouseLease houseLease) {
		// 删除位
		houseLease.setDelFlg(false);;
		// 更新
		iHouseLeaseService.updateById(houseLease);
		return ResultInfo.success();
	}
	@Override
	public ResultInfo getList(String des, Integer provienceId, Integer cityId, Integer reignId, Integer clientId,
			Integer leaseId, Integer houseAreaId, Integer leasePriceId, String tel, String wxNumber, String louceng,
			String xiaoqu, String pianqu, String chaoxiang, String zhuangxiu, Integer dianti, String niandai,
			Integer roomCount, Integer currentPage, Integer pageSize, String sort, Boolean dec) {
		// TODO Auto-generated method stub
System.out.println("+++++++++++++"+ currentPage);
		
		QueryWrapper<HouseLeaseClientVo> queryWrapper = new QueryWrapper<HouseLeaseClientVo>();
		queryWrapper.eq("sale_flg", "1");
		// -----------判断描述不为空的情况下---------- 按描述搜索
		if (StringUtils.hasLength(des)) {
			queryWrapper.like("des", des);
		}
		// ----------- 判断面积区间id不为空的情况下----------- 按面积区间检索
		if (null != houseAreaId && houseAreaId != 0) {
		// 得到1个面积区间对象
			HouseArea houseArea = iHouseAreaService.getById(houseAreaId);
		// 在条件中做检索 在面积区间的最小-最大之间做查询
			queryWrapper.between("area", houseArea.getMinArea(), houseArea.getMaxArea());
		// queryWrapper.eq("area", area);//按面积搜索
		}
		// --------------判断价格区间id不为空且不为0-----------按照价格区间检索
		if (null != leasePriceId && leasePriceId != 0) {
		HouseLeasePrice houseLeasePrice = iHouseLeasePriceService.getById(leasePriceId);
			queryWrapper.between("lease_price", houseLeasePrice.getMinPrice(), houseLeasePrice.getMaxPrice());
		}
			queryWrapper.eq("del_flg", "1");
		
		//----------------判断片区是否为空-------
		if(pianqu != null && pianqu !=""){
			queryWrapper.like("pianqu",pianqu);
		}
		
		if(xiaoqu != null && xiaoqu != ""){
			queryWrapper.like("xiaoqu",xiaoqu);
		}
		if(null != dianti && 0 != dianti){
			queryWrapper.like("dianti",dianti);
		}
		
		if(zhuangxiu != null && zhuangxiu != ""){
			queryWrapper.like("zhuangxiu", zhuangxiu);
		}
		
		if(provienceId != null && provienceId != 0){
		System.out.println("_____________________" + provienceId);
			queryWrapper.eq("provience_id", provienceId);
		}
		
		if(cityId != null && cityId != 0){
			queryWrapper.eq("city_id", cityId);
		}
		
		if(reignId != null && reignId != 0){
			queryWrapper.eq("reign_id", reignId);
		}
		
		if(roomCount != null){
			queryWrapper.eq("room_count", roomCount);
		}
		if(louceng != null){
			queryWrapper.like("louceng", louceng);
		}
		if(chaoxiang != null){
			queryWrapper.like("chaoxiang", chaoxiang);
		}
		if (clientId != null && clientId != 0) {
			queryWrapper.eq("client_id",clientId);
		}
		//判断排序条件order是否为空
				if(StringUtils.hasLength(sort)) {
					if (!sort.equals("default")) {
						if(dec) {
							queryWrapper.orderByDesc(sort);
						}
						else {
							queryWrapper.orderByAsc(sort);
						}
					}
					else {
						queryWrapper.orderByAsc("lease_id");
					}
				}
		
		IPage<HouseLeaseClientVo> pageInfo = new Page<HouseLeaseClientVo>(currentPage, pageSize);
		return ResultInfo.success(this.page(pageInfo, queryWrapper));
	}
}
